<template>
    <div>
<!--        <h2>pdf预览页面</h2>-->
<!--        <div class="wrap">-->
<!--            <iframe-->
<!--                    v-if="showPdf"-->
<!--                    id="previewPdf"-->
<!--                    :src= pSrc-->
<!--                    height="900"-->
<!--                    width="100%"-->
<!--            >-->
<!--            </iframe>-->

<!--        </div>-->

    </div>
</template>

<script>

    export default {
        name: "pdfReview",
        data(){
            return{
                showPdf: false,
                fileUrl: '',
                pSrc:'',
            }
        },
        mounted() {
            this.viewPdf()
            //this.convertDataURL()
        },
        methods: {

            // convertDataURL(){
            //     var pdfHeader = "data:application/pdf;base64"
            //     this.base64 = pdfHeader+this.base64
            // },


            //简历预览
            viewPdf() {
                const content = localStorage.getItem('pdfbase64')
                let url = this.createDownloadFileUrl('pdf预览', content)
                console.log(url)
                //this.pSrc = url
                window.open(url)
            },
            createDownloadFileUrl(fileName, file) {
                var blob = this.dataURLtoFile(
                    `data:application/pdf;base64,${file}`,
                    fileName
                ) // application/zip 需要改成要下载文件的类型
                blob.lastModifiedDate = new Date()
                blob.name = fileName
                return URL.createObjectURL(blob)
            },
            dataURLtoFile(dataurl, filename) {
                // 生成Blob
                var arr = dataurl.split(',')
                var mime = arr[0].match(/:(.*?);/)[1]
                var bstr = atob(arr[1])
                var n = bstr.length
                var u8arr = new Uint8Array(n)
                while (n--) {
                    u8arr[n] = bstr.charCodeAt(n)
                }
                return new Blob([u8arr], {type: mime})
            },
            // 这是打开本地文件进行预览(上传)
            preview() {
                let files = document.getElementById('myfile').files[0]
                if (files.type !== 'application/pdf') {
                    alert('只能上传一份pdf文件哦～')
                    return
                }
                this.showPdf = true
                this.fileUrl = this.getObjectURL(files)
            },
            getObjectURL(file) {
                let url = null
                if (window.createObjectURL != undefined) {
                    // basic
                    url = window.createObjectURL(file)
                } else if (window.webkitURL != undefined) {
                    // webkit or chrome
                    url = window.webkitURL.createObjectURL(file)
                } else if (window.URL != undefined) {
                    // mozilla(firefox)
                    url = window.URL.createObjectURL(file)
                }
                return url
            }
        }

    }
</script>

<style scoped>
    .wrap{
        width: 1360px;
        height: 500px;
    }
</style>
